import requests
import re
from bs4 import BeautifulSoup
import pandas as pd

url = ['54161','54371','50936','54172','54260','50949','54157','54363','71532']
cart = ['长春','白山','白城','吉林','辽源','松原','四平','通化','延边']

df1 = pd.DataFrame()
df2 = pd.DataFrame()
df3 = pd.DataFrame()
df4 = pd.DataFrame()
for j in range(len(url)):
    req = requests.get("https://tianqi.2345.com/air-{}.htm".format(url[j]))
    obj = re.compile(
        r'var hoursTxt48 = \[(?P<hoursTxt48>.*?)\],.*?'
                    r'hoursData48 = \[(?P<hoursData48>.*?)\];.*?'    #未来48小时
                    r'var hoursTxt24 = \[(?P<hoursTxt24>.*?)\],.*?'
                    r'hoursData24 = \[(?P<hoursData24>.*?)\];.*?'    #过去24小时
                    r'var daysTxt15 = \[(?P<daysTxt15>.*?)\],.*?'
                    r'daysData15 = \[(?P<daysData15>.*?)];.*?'    #未来15天
                    r'var daysTxt30 = \[(?P<daysTxt30>.*?)\],.*?'
                    r'daysData30 = \[(?P<daysData30>.*?)];'   #未来30天
                    , re.S)
    result = obj.finditer(req.text)
    for i in result:
        hoursTxt48 = i.group('hoursTxt48').encode().decode("unicode_escape").split("\",\"")
        hoursTxt48[0] = hoursTxt48[0][1:]
        hoursTxt48[-1] = hoursTxt48[-1][:-1]
        hoursData48 = i.group('hoursData48').split("\",\"")
        hoursData48[0] = hoursData48[0][1:]
        hoursData48[-1] = hoursData48[-1][:-1]
        hoursTxt24 = i.group('hoursTxt24').encode().decode("unicode_escape").split("\",\"")
        hoursTxt24[0] = hoursTxt24[0][1:]
        hoursTxt24[-1] = hoursTxt24[-1][:-1]
        hoursData24 = i.group('hoursData24').split("\",\"")
        hoursData24[0] = hoursData24[0][1:]
        hoursData24[-1] = hoursData24[-1][:-1]
        daysTxt15 = i.group('daysTxt15').encode().decode("unicode_escape").split("\",\"")
        daysTxt15[0] = daysTxt15[0][1:]
        daysTxt15[-1] = daysTxt15[-1][:-1]
        daysData15 = i.group('daysData15').split("\",\"")
        daysData15[0] = daysData15[0][1:]
        daysData15[-1] = daysData15[-1][:-1]
        daysTxt30 = i.group('daysTxt30').encode().decode("unicode_escape").split("\",\"")
        daysTxt30[0] = daysTxt30[0][1:]
        daysTxt30[-1] = daysTxt30[-1][:-1]
        daysData30 = i.group('daysData30').split("\",\"")
        daysData30[0] = daysData30[0][1:]
        daysData30[-1] = daysData30[-1][:-1]
    df1[cart[j]] = hoursData48
    df2[cart[j]] = hoursData24
    df3[cart[j]] = daysData15
    df4[cart[j]] = daysData30
df2.index = hoursTxt24
df3.index = daysTxt15    
df4.index = daysTxt30    
df1.index = hoursTxt48
df1.to_csv('数据爬虫/未来48小时.csv')
df2.to_csv('数据爬虫/过去24小时.csv')
df3.to_csv('数据爬虫/未来15天.csv')
df4.to_csv('数据爬虫/过去30天.csv')